package com.springcloud.sc.ucpv5.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.springcloud.sc.ucp.v5.api.entity.UcpVoipVoiceRecord;
import com.springcloud.sc.ucp.v5.api.entity.UcpVoipVoiceRecordRelation;
import com.springcloud.sc.ucp.v5.api.vo.UcpVoipVoiceVo;
import com.springcloud.sc.ucpv5.client.commondata.MyUser;
import com.springcloud.sc.ucpv5.client.module.datamanager.BaseDataManager;
import com.springcloud.sc.ucpv5.mapper.UcpVoipVoiceRecordMapper;
import com.springcloud.sc.ucpv5.service.UcpVoipVoiceRecordRelationService;
import com.springcloud.sc.ucpv5.service.UcpVoipVoiceRecordService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.Topic;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@AllArgsConstructor
public class UcpVoipVoiceRecordServiceImpl extends ServiceImpl<UcpVoipVoiceRecordMapper, UcpVoipVoiceRecord> implements UcpVoipVoiceRecordService {

    @Autowired
    private UcpVoipVoiceRecordMapper ucpVoipVoiceRecordMapper;

    @Autowired
    private JmsTemplate jmsTemplate;

    @Resource(name = "topic")
    private Topic topic;

    @Autowired
    private UcpVoipVoiceRecordRelationService ucpVoipVoiceRecordRelationService;


    @Override
    public Integer saveUcpVoipVoiceRecord(UcpVoipVoiceVo ucpVoipVoiceVo) {
        UcpVoipVoiceRecord ucpVoipVoiceRecord = new UcpVoipVoiceRecord();
        BeanUtils.copyProperties(ucpVoipVoiceVo, ucpVoipVoiceRecord);
        LocalTime localTime = LocalTime.now();
        LocalDate localDate = LocalDate.now();
        ucpVoipVoiceRecord.setCreateTime(localDate.toString() +" "+ localTime.toString() );
        // 20190524 wsy 增加用户描述
        try{
            MyUser user = BaseDataManager.getInstance().userGet(ucpVoipVoiceRecord.getCalledNumber());
            if(user != null){
                ucpVoipVoiceRecord.setCalledName(user.getIdlData().getDesc());
            }
            else{
                ucpVoipVoiceRecord.setCalledName(ucpVoipVoiceRecord.getCalledNumber());
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return ucpVoipVoiceRecordMapper.insert( ucpVoipVoiceRecord );
    }

    @Override
    public List<UcpVoipVoiceRecord> queryVoipVoiceRecord(String uuid) {
        return ucpVoipVoiceRecordMapper.queryVoipVoiceRecord(uuid);
    }

    public List queryVoipVoiceRecordList(JSONObject jsonObject){

        Map map = new HashMap();
        map.put("uuid" , jsonObject.get("uuid") );
        map.put("calledNumber" , jsonObject.get("calledNumber") );

        return ucpVoipVoiceRecordMapper.queryVoipVoiceRecordList( map );
    }

    public Map saveVoipVoiceTaskRecord(JSONObject jsonObject) {
        Map map = new HashMap();
        UcpVoipVoiceVo ucpVoipVoiceVo = new UcpVoipVoiceVo();
        String uuid =  String.valueOf( jsonObject.get("uuid") );
        String calledNumber =String.valueOf( jsonObject.get("calledNumber") );
        String callStartTime =String.valueOf( jsonObject.get("callStartTime") );
        String callRetryCount =String.valueOf( jsonObject.get("callRetryCount") );
        String callResponseTime =String.valueOf( jsonObject.get("callResponseTime") );
        String callEndTime =String.valueOf( jsonObject.get("callEndTime") );
        String result =String.valueOf( jsonObject.get("result") );
        if(uuid==null || uuid.equals("null") || uuid.equals("")){
            map.put("result_code","1001");
            map.put("result_text","uuid参数为空");
            return map;
        }
        if(result == null || result.equals("null")){
            ucpVoipVoiceVo.setResult( 0 );
        }else{
            Integer integerResult = Integer.valueOf(result);
            ucpVoipVoiceVo.setResult( integerResult );
        }
        String branchUUid = null;
        if(uuid.indexOf("_")>0){
            branchUUid = uuid.split("_")[0];
        }else{
            branchUUid = uuid;
        }
        ucpVoipVoiceVo.setUuid( branchUUid );
        ucpVoipVoiceVo.setCalledNumber( calledNumber );
        ucpVoipVoiceVo.setCallStartTime( callStartTime );
        ucpVoipVoiceVo.setCallRetryCount( callRetryCount );
        ucpVoipVoiceVo.setCallResponseTime( callResponseTime );
        ucpVoipVoiceVo.setCallEndTime( callEndTime );

        List<UcpVoipVoiceRecordRelation> ucpVoipVoiceRecordRelationList = ucpVoipVoiceRecordRelationService.getEntityByUUidCalledNumber(branchUUid ,  calledNumber);
        UcpVoipVoiceRecordRelation ucpVoipVoiceRecordRelation = null;
        LocalTime localTime = LocalTime.now();
        LocalDate localDate = LocalDate.now();
        if(ucpVoipVoiceRecordRelationList.isEmpty()){
            ucpVoipVoiceRecordRelation = new UcpVoipVoiceRecordRelation();
            ucpVoipVoiceRecordRelation.setUuid( branchUUid );
            ucpVoipVoiceRecordRelation.setCalledNumber( calledNumber );
            ucpVoipVoiceRecordRelation.setCallStartTime( callStartTime );
            ucpVoipVoiceRecordRelation.setCallStartTime( localDate.toString() +" "+ localTime.toString() );
            // 20190524 wsy 增加用户描述
            try{
                MyUser user = BaseDataManager.getInstance().userGet(calledNumber);
                if(user != null){
                    ucpVoipVoiceRecordRelation.setCalledName(user.getIdlData().getDesc());
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }else{
            ucpVoipVoiceRecordRelation = ucpVoipVoiceRecordRelationList.get(0);
        }
        ucpVoipVoiceRecordRelation.setCallRetryCount( callRetryCount );
        ucpVoipVoiceRecordRelation.setCallEndTime( callEndTime );
        ucpVoipVoiceRecordRelation.setResult( result );
        ucpVoipVoiceRecordRelationService.saveOrUpdate( ucpVoipVoiceRecordRelation );
        Integer ts= this.saveUcpVoipVoiceRecord( ucpVoipVoiceVo );
        boolean bool = SqlHelper.retBool(ts);
        if(bool){
            Map topicMap = new HashMap();
            topicMap.put("type","VoipVoiceRecord" );
            topicMap.put("uuid",branchUUid );
            topicMap.put("calledNumber",calledNumber);
            topicMap.put("callStartTime",callStartTime);
            topicMap.put("callRetryCount",callRetryCount);
            topicMap.put("callResponseTime",callResponseTime);
            topicMap.put("callEndTime",callEndTime);
            topicMap.put("result",result);
            Object json = JSONArray.toJSON(  topicMap );
            System.out.println(json.toString());
            //操作成功，开始发MQ数据
            jmsTemplate.convertAndSend(topic,json.toString());
        }

        if(bool){
            map.put("result_code","0");
            map.put("result_text","操作成功");
        }else{
            map.put("result_code","1001");
            map.put("result_text","参数错误");
        }
        return map;
    }
}
